/*
 * Created by: 
 * Created: Sunday, December 17, 2006
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using Dune.Business.Domain.Users;
using Edge.Business.Domain;
using Edge.Common.ComponentModel;
using Edge.Common.Utils;
using Edge.Common.ComponentModel;
using Edge.Common.Utils;

namespace Edge.Common.Web.Naming
{
    public class EntityDescriptionsService : IEntityDescriptionsService
    {
        protected bool ShortText(object obj, out string shortText)
        {
            shortText = string.Empty;

            if (obj == null)
                throw new ArgumentException("obj");

            if (obj is User)
            {
                User contact = (User)obj;
                shortText = contact.Email;
            }
            else if (obj is PlaceHolder)
            {
                PlaceHolder ph = (PlaceHolder)obj;
                shortText = ph.Name;
            }
            else if (obj is Enum)
            {
                // TODO use Description attribute
                shortText = obj.ToString();
            }
            return shortText != string.Empty;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj">not null</param>
        /// <returns></returns>
        public virtual string GetShortText(object obj)
        {
            string ret = string.Empty;
            if (ShortText(obj, out ret))
            {
                return ret;
            }
            else // else return Id value
            {
                object id = null;
                try
                {
                    id = ReflectionUtils.GetPublicPropertyValue(obj, "Id");
                }
                catch (ArgumentException ex)
                {
                    // do nothing
                }
                return id == null ? "please add switch to " + typeof(EntityDescriptionsService) + ".GetShortText for " + obj.GetType() : id.ToString();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="type">not null</param>
        /// <returns></returns>
        public string GetEntityTypeDescription(Type type)
        {
            if (type == null)
                throw new ArgumentException("type");

            DescriptionAttribute descriptionAttribute = ReflectionUtils.GetFirstAttribure<DescriptionAttribute>(type, true);
            if (descriptionAttribute != null)
            {
                return descriptionAttribute.Description;
            }
            else
                return type.Name;
        }

        /// <summary>If an attribute such as is on an enumeration exists, this will return that 
        /// information</summary>
        /// <param name="value">The object which has the attribute.</param>
        /// <returns>The description string of the attribute or string.empty</returns>
        public string GetEnumDescription(object value)
        {
            string retVal = string.Empty;
            try
            {
                FieldInfo fieldInfo = value.GetType().GetField(value.ToString());
                DescriptionAttribute[] attributes =
                   (DescriptionAttribute[])fieldInfo.GetCustomAttributes
                   (typeof(DescriptionAttribute), false);
                retVal = ((attributes.Length > 0) ? attributes[0].Description : value.ToString());
            }
            catch (NullReferenceException)
            {
                //Occurs when we attempt to get description of an enum value that does not exist
            }
            finally
            {
                if (string.IsNullOrEmpty(retVal))
                    retVal = "Unknown";
            }

            return retVal;
        }

    }
}
